<template>
    <LmsCard icon="lmsfont lms-icon-jilu2" name="生成代码">
        <LmsTable :search-box="searchBox" :columns="columns" :actions="actions" :query="selectFunc" ref="table">
            <template #dialog>
                <Detail v-model="showDetail" :table-list="tableList"/>
            </template>
        </LmsTable>
    </LmsCard>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import api from '@/api/gen/index'
import { ElMessage, ElMessageBox } from 'element-plus'
import Detail from './components/CreateCodeDetail.vue'

const table = ref()

const showDetail = ref(false)

const tableList = ref<any[]>([])

const schemaItems = ref<any[]>()

const searchBox = ref([
    { id: 'schema', name: '模式', type: 'selection', items: schemaItems}
])

onMounted(async () => {
    const res = await api.listSchema()
    if (res.ok) {
        schemaItems.value = res.data.map((m: any) => {
            return { value: m.schemaName, text: m.schemaName }
        })
    }
})

const actions = [
    { name: '生成代码', icon: 'lmsfont lms-icon-xinzeng', onclick: createCode },
]

const columns = [
    { id: 'tableSchema', name: '模式' },
    { id: 'tableName', name: '表名' },
    { id: 'tableComment', name: '表注释'}
]

const selectFunc = async (params: any) => {
    const res = await api.query(params)
    if (res.ok) {
        return res.data
    }
}

function createCode() {
    const rows = table.value.getSelectionRows()
    if (rows.length == 0) {
        return ElMessage({
            type: 'warning',
            message: '请至少选择一条数据！'
        })
    }
    ElMessageBox.confirm('确认生成所选表格对应的代码吗？', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true
    }).then(async () => {
        showDetail.value = true
        tableList.value = rows
    })
}
</script>